package com.ebt.m.users;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.commons.utils.NetUtils;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.users.bean.CardData;
import com.ebt.m.users.bean.CardService;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by andy.wang on 2016/12/14.
 */

public class CardServicePresenter extends BaseListPresenter<BaseListContract.IView> {
    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    public static final int TYPE_ADD = 1;
    public static final int TYPE_SERVICE = 2;


    public CardServicePresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        EBTAPI opportunityApi = AppInitService.getEbtApi();
        opportunityApi.getCardData()
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(response -> iView.updateData(createSectionItem(response.getData()))
                        , throwable -> {
                            throwable.printStackTrace();
                            NetUtils.checkHttpException(mContext, throwable);
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew();
    }

    @Override
    public void loadNext(Object... args) {
    }

    public List<SectionItem> createSectionItem(CardData cardData) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        if (cardData == null || cardData.getService() == null || cardData.getService().size() == 0) {
            return sections;
        }
        try {
            SectionItem infoItem = new SectionItem(TYPE_ADD, "");
            sections.add(infoItem);
            for (CardService cardService : cardData.getService()) {
                SectionItem sectionItem = new SectionItem(TYPE_SERVICE, cardService);
                sections.add(sectionItem);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }

    public void deleteItem(CardService cardService) {
        EBTAPI ebtapi = AppInitService.getEbtApi();
        iView.showProgress(true);
        ebtapi.deleteCarddataItem(cardService.getAttachmentId())
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(responseBodyResponse -> {
                    iView.showProgress(false);
                    try {
                        JSONObject jsonObject = new JSONObject(responseBodyResponse.body().string());
                        long num = jsonObject.getJSONObject("data").getLong("num");
                        if (num > 0)
                            refresh();
                    } catch (Exception e) {
                    }
                }, throwable -> {
                    iView.showProgress(false);
                    Logger.e(throwable.getMessage());
                    NetUtils.checkHttpException(mContext, throwable);
                });
    }


}
